package com.yugao.fintech.draper.admin.service.impl;

import cn.hutool.core.lang.Dict;
import com.yugao.fintech.draper.admin.service.SocialAppService;
import com.yugao.fintech.draper.admin.service.SocialOperationService;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.security.util.SecurityUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SocialAppServiceImpl implements SocialAppService {
    @Autowired
    private List<SocialOperationService> socialOperationServices;

    /**
     * 获取第三方平台的绑定信息
     *
     * @return 第三方平台的绑定信息
     */
    @Override
    public Dict bindSocialApp() {
        SecurityUser user = SecurityUserUtil.getCurrentUser();
        Long userId = user.getUserId();
        Dict dict = Dict.create();
        socialOperationServices.forEach(t -> dict.put(t.support(), t.getBindInfo(userId)));
        return dict;
    }

    /**
     * 解除绑定
     *
     * @param type 三方平台类型
     */
    @Override
    public void unBind(String type) {
        Optional<SocialOperationService> optional = socialOperationServices.stream()
                .filter(t -> t.support().equals(type))
                .findFirst();
        optional.ifPresent(SocialOperationService::unbind);
    }
}
